<template>
  <!-- 保存病历模板 -->
  <el-dialog title="存储病历模板" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form label-width="140px" ref="form" :model="form" :rules="rules">
      <el-form-item label="模板名称：" prop="templateName">
        <el-input v-model="form.templateName"></el-input>
      </el-form-item>
      <el-form-item label="模板类型：" prop="refType">
        <el-radio-group v-model="form.refType">
          <el-radio v-for="(item,index) in typeArr" :key="index" :label="item.id">{{item.desc}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <blForm ref="blForm" :dataForm="newDataForm" :doctorId="doctorId" :deptId="deptId" :cureFirstVisitRecord="[]" @submit="blFormSubmit">
      <span slot="footer">
        <el-button type="primary" native-type="submit" :loading="btnLoading">确 定</el-button>
      </span>
    </blForm>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "dataForm", "doctorId", "deptId"],
  components: {
    blForm: () => import("./blForm.vue")
  },
  data() {
    return {
      btnLoading: false,
      newDataForm: [],
      form: {
        hpId: "",
        templateType: 1,
        templateName: "",
        refType: 1,
        refId: "",
        dynamicField: "",
        id: ""
      },
      rules: {
        templateName: [{ required: true, message: "此项必填", trigger: "blur" }]
      },
      typeArr: []
    };
  },
  mounted() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getCommonConfigByKey("模板类型").then(res => {
      th.typeArr = res;
    });
  },
  methods: {
    //提交
    blFormSubmit(obj) {
      let th = this;
      let j = {};
      let add = function() {
        th.btnLoading = true;
        th.$api
          .addtemplateDynamic(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("操作完成");
              th.onClose();
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(res => {
            th.btnLoading = false;
          });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          th.form.dynamicField = JSON.stringify(obj);
          Object.assign(j, th.form);
          if (th.form.refType == 0) j.refId = th.deptId;
          else j.refId = th.doctorId;
          if (th.form.id) {
            update();
          } else {
            add();
          }
        }
      });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        //排除发病日期，和初步诊断
        let arr = [];
        for (let index = 0; index < th.dataForm.length; index++) {
          let el = th.dataForm[index];
          if (el.name == "发病日期" || el.name == "初步诊断") {
            continue;
          }
          arr.push(el);
        }
        th.newDataForm = arr;
      });
    },
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>
